package com.happyscan138.makefun;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.happyscan138.makefun.Urls.Urls;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;

import org.json.JSONException;
import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

import static com.happyscan138.makefun.base.http.client;
import static com.happyscan138.makefun.base.http.params;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2016/12/9.
 */

public class ModifyPassword extends AppCompatActivity implements View.OnClickListener{
    private ImageButton back;
    private Button buttonSave;
    private EditText oldPassword,newPassword,enterNewPassword;
    private String oldPwd,newPwd,enterNewPwd;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    private String access_key;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.modify_password);
        client=new AsyncHttpClient();
        params=new RequestParams();
        preferences = getSharedPreferences("UserInfo", MODE_PRIVATE);
        access_key=preferences.getString("UserId","");
        initView();
    }
    public void initView(){
        Textwacher change=new Textwacher();
        back=(ImageButton)findViewById(R.id.ModifyPassword_Back);
        back.setOnClickListener(this);
        buttonSave=(Button)findViewById(R.id.ModifyPassword_Save);
        buttonSave.setOnClickListener(this);
        oldPassword=(EditText)findViewById(R.id.ModifyPassword_OldPwd);
        oldPassword.addTextChangedListener(change);
        newPassword=(EditText)findViewById(R.id.ModifyPassword_NewPwd);
        newPassword.addTextChangedListener(change);
        enterNewPassword=(EditText)findViewById(R.id.ModifyPassword_EnterNewPwd);
        enterNewPassword.addTextChangedListener(change);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ModifyPassword_Back:
                finish();
                break;
            case R.id.ModifyPassword_Save:
                params.put("access_key",access_key);
                params.put("old_pwd",oldPwd);
                params.put("new_pwd",newPwd);
                params.put("confirm_pwd",enterNewPwd);
                log.e("params",params.toString());
                RequestHandle handle=client.post(Urls.ModifyPassword,params,new JsonHttpResponseHandler(){
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        String jsonStr=response.toString();
                        try {
                            JSONObject jsonObject=new JSONObject(jsonStr);
                            String msg=jsonObject.getString("msg");
                            Toast.makeText(ModifyPassword.this,msg,Toast.LENGTH_SHORT).show();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                        super.onFailure(statusCode, headers, responseString, throwable);
                    }
                });
                break;
        }
    }

    class Textwacher implements TextWatcher{

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            oldPwd=oldPassword.getText().toString();
            newPwd=newPassword.getText().toString();
            enterNewPwd=enterNewPassword.getText().toString();
        }
    }
}
